package test.freshui.ffmpeg;   
  
import android.content.Context;   
import android.graphics.Canvas;   
import android.graphics.Color;   
import android.graphics.Paint;   
import android.view.SurfaceHolder;   
import android.view.SurfaceView;   
import android.view.SurfaceHolder.Callback;   
  
public class mySurfaceView extends SurfaceView implements Callback {   
  
    GameThread thread ;   
    public mySurfaceView(Context context) {   
        super(context);   
        SurfaceHolder holder = getHolder();   
        holder.addCallback(this);   
        thread = new GameThread(holder);   
    }   
  
    @Override  
    public void surfaceChanged(SurfaceHolder holder, int format, 
    		int width, int height) {   
        // TODO Auto-generated method stub   
    }   
  
    @Override  
    public void surfaceCreated(SurfaceHolder holder) {   
        // TODO Auto-generated method stub   
        thread.start();   
    }   
  
    @Override  
    public void surfaceDestroyed(SurfaceHolder holder) {   
        // TODO Auto-generated method stub   
        thread.run = false;   
    }   
       
    class GameThread extends Thread{   
        SurfaceHolder surfaceHolder;   
        boolean run = true;   
        public GameThread(SurfaceHolder surfaceHolder){   
            this.surfaceHolder = surfaceHolder;   
        }   
           
        public void run(){   
            int i = 0;   
            Canvas c = null;   
            Paint paint = new Paint();   
            paint.setColor(Color.RED);   
            while(run){   
                try{   
                    c = surfaceHolder.lockCanvas();   
                    synchronized (surfaceHolder) {   
                        c.drawText(i+"", 20, 20, paint);   
                        i++;   
                        Thread.sleep(1000);   
                    }   
                }   
                catch (Exception e) {   
                    e.printStackTrace();   
                }   
                finally{   
                    if(c != null){   
                        surfaceHolder.unlockCanvasAndPost(c);   
                    }   
                }   
            }   
        }   
    }   
}  